package com.android.nearshop.app.activity.main;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.nearshop.R;
import com.android.nearshop.app.activity.map.SelLocActivity;
import com.android.nearshop.app.activity.map.ShopLocationActivity;
import com.android.nearshop.business.location.MyBMapManager;
import com.android.nearshop.business.model.Addr;
import com.android.nearshop.io.database.SearKeyDB;
import com.android.nearshop.io.protocol.ProtocolManager;
import com.android.nearshop.io.protocol.in.AreaListAction;
import com.android.nearshop.io.protocol.in.BannerAction;
import com.android.nearshop.io.protocol.in.BannerAction.ADV_TYPE;
import com.android.nearshop.io.protocol.in.CateListAction;
import com.android.nearshop.io.protocol.in.CommentListAction;
import com.android.nearshop.io.protocol.in.CountAction;
import com.android.nearshop.io.protocol.in.FindShopAction;
import com.android.nearshop.io.protocol.in.ShopDetailAction;
import com.android.nearshop.io.protocol.in.ShopDisCountListAction;
import com.android.nearshop.io.protocol.in.SoapAction;
import com.android.nearshop.io.protocol.in.SoapAction.ActionListener;
import com.android.nearshop.io.protocol.in.VersionAction;
import com.android.nearshop.io.protocol.out.Adv;
import com.android.nearshop.io.protocol.out.AdvList;
import com.android.nearshop.io.protocol.out.Area;
import com.android.nearshop.io.protocol.out.AreaList;
import com.android.nearshop.io.protocol.out.Catalogy;
import com.android.nearshop.io.protocol.out.CatalogyList;
import com.android.nearshop.io.protocol.out.CommentList;
import com.android.nearshop.io.protocol.out.DisShopList;
import com.android.nearshop.io.protocol.out.ShakeCount;
import com.android.nearshop.io.protocol.out.ShopDetail;
import com.android.nearshop.io.protocol.out.ShopList;
import com.android.nearshop.io.protocol.out.Version;
import com.android.nearshop.util.Constant;
import com.android.nearshop.util.Util;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-6
 * @see : 主界面
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class MainActivity extends Activity implements OnClickListener
{
    
    private EditText param1;
    private EditText param2;
    private EditText param3;
    private Button   btn_ok;
    private TextView showResult;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        param1 = (EditText)findViewById(R.id.param1);
        param2 = (EditText)findViewById(R.id.param2);
        param3 = (EditText)findViewById(R.id.param3);
        showResult = (TextView)findViewById(R.id.showResult);
        btn_ok = (Button)findViewById(R.id.btn_ok);
        
        param1.setText("TCL大厦");
        param2.setText("114.070000");
        param3.setText("22.620000");
        
        btn_ok.setOnClickListener(this);
        SearKeyDB.test();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        
        if(requestCode == SelLocActivity.CODE_REQUEST)
        {
            if(resultCode == Activity.RESULT_OK)
            {
                Addr addr = (Addr)data.getSerializableExtra(Constant.TAG_ADDR);
                showResult.setText(addr.toString());
            }else
            {
                showResult.setText("user cancel sel location");
            }
        }else
        if(requestCode == SelCityActivity.CODE_REQUEST)
        {
            if(resultCode == Activity.RESULT_OK)
            {
                Area addr = (Area)data.getSerializableExtra(Constant.TAG_CITY);
                showResult.setText(addr.toString());
            }else
            {
                showResult.setText("user cancel sel location");
            }
        }else
        if(requestCode == SelCategoryActivity.CODE_REQUEST)
        {
            if(resultCode == Activity.RESULT_OK)
            {
                ArrayList<Integer> cates = data.getIntegerArrayListExtra(Constant.TAG_CATEGORYS);
                StringBuffer buf = new StringBuffer();
                for(int one : cates)
                {
                    buf.append(one+",");
                }
                showResult.setText(buf.toString());
            }else
            {
                showResult.setText("user cancel sel location");
            }
        }
    }
    
    @Override
    public void onClick(View v)
    {        
//        testAction();
        testMainIndex();
//        testSelLocMap();
//        testMap();
//        testSelCity();
//        testSelCate();
    }
    
    // 测试主界面
    protected void testMainIndex()
    {
        MainIndexActivity.startActivity(this,MainIndexActivity.TAB_INDEX_5);
        finish();
    }
    
    // 测试地图
    protected void testMap()
    {
          Addr param = new Addr();
          String str = "";
          str = param1.getEditableText().toString();
          param.address = str;
          str = param2.getEditableText().toString();
          param.longitude = Double.parseDouble(str);
          str = param3.getEditableText().toString();
          param.latitude = Double.parseDouble(str);
          ShopLocationActivity.startActivity(this, param);
    }
    
    protected void testSelLocMap()
    {
        SelLocActivity.startActivityResult(this);
    }
    
    protected void testSelCity()
    {
        SelCityActivity.startActivityResult(this);
    }
    
    protected void testSelCate()
    {
        SelCategoryActivity.startActivityResult(this, true);
    }
    
    // 测试API
    protected void testAction()
    {
        SoapAction<?> action = null;
//        action = testAreaListAction(); // OK
//        action = testBannerAction();   // NoResult
//        action = testVersionAction();  // Fail NestablException
//        action = testCateListAction(); // OK
//        action = testCountAction();    // OK
//        action = testFindShopAction(); // OK
//        action = testShopDetailAction(); // NoResult
//        action = testshopDisCountListAction();// NoResult
//        action = testCommentListAction(); // NoResult
        ProtocolManager.getProtocolManager().submitAction(action);
    }
    
    // 城镇区域测试
    protected SoapAction<?> testAreaListAction()
    {
        AreaListAction action = new AreaListAction();
        action.setActionListener(new AreaListAction.ActionListener<AreaList>()
        {
            @Override
            public void onSucceed(AreaList result)
            {
                StringBuilder buf = new StringBuilder();
                if( result != null && result.list!=null && result.list.size() > 0)
                {
                    for(Area one : result.list)
                    {
                        buf.append(one.areaId + ":" + one.areaName + "\n");
                    }
                    showResult.setText(buf.toString());
                }else
                {
                    showResult.setText(" no result!");
                }                
            }

            @Override
            public void onError(int resultCode)
            {
                showResult.setText(resultCode+"");
            }
        });
        return action;
    }
    // 商家的各级行业列表测试
    protected SoapAction<?> testCateListAction()
    {
        // 720
        CateListAction action = new CateListAction(0);
        action.setActionListener(new CateListAction.ActionListener<CatalogyList>()
        {
            @Override
            public void onSucceed(CatalogyList result)
            {
                StringBuilder buf = new StringBuilder();
                if(result != null && result.list!=null && result.list.size() > 0)
                {
                    for(Catalogy one : result.list)
                    {
                        buf.append(one.catalogyId + ":" + one.catalogyName + "\n");
                    }
                    showResult.setText(buf.toString());
                }else
                {
                    showResult.setText(" no result!");
                }
            }

            @Override
            public void onError(int resultCode)
            {
                showResult.setText(resultCode+"");
            }
        });
        return action;
    }
    // 广告列表测试
    protected SoapAction<?> testBannerAction()
    {
        BannerAction action = new BannerAction(ADV_TYPE.SUB);
        action.setActionListener(new BannerAction.ActionListener<AdvList>()
        {
            @Override
            public void onSucceed(AdvList result)
            {
                StringBuilder buf = new StringBuilder();
                if(result != null && result.list!=null && result.list.size() > 0)
                {
                    for(Adv one : result.list)
                    {
                        buf.append(one.advId + ":" + one.advImgUrl + "\n");
                    }
                    showResult.setText(buf.toString());
                }else
                {
                    showResult.setText(" no result!");
                }                
            }

            @Override
            public void onError(int resultCode)
            {
                showResult.setText(resultCode+"");
            }
        });
        return action;
    }
    // 版本测试
    protected SoapAction<?> testVersionAction()
    {
        VersionAction action = new VersionAction();
        action.setActionListener(new ActionListener<Version>()
        {
            @Override
            public void onSucceed(Version result)
            {
                if(result != null)
                {
                    showResult.setText(result.versionCode + ":" + result.downUrl);
                }else
                {
                    showResult.setText(" no result!");
                }
            }

            @Override
            public void onError(int resultCode)
            {
                showResult.setText(resultCode+"");
            }
        });
        return action;
    }
    // 摇摇统计测试
    protected SoapAction<?> testCountAction()
    {
        CountAction action = new CountAction();
        action.setActionListener(new CountAction.ActionListener<ShakeCount>()
        {
            @Override
            public void onSucceed(ShakeCount result)
            {     
                if(result != null)
                {
                    showResult.setText(result.count + " shakes");
                }else
                {
                    showResult.setText(" no result!");
                }
            }

            @Override
            public void onError(int resultCode)
            {
                showResult.setText(resultCode+"");
            }
        });
        return action;
    }
    // 查询商家测试
    protected SoapAction<?> testFindShopAction()
    {
        MyBMapManager mamager = MyBMapManager.getInstace(this);
        if(!mamager.isLocating() && mamager.getMyLoation() == null)
        {
            MyBMapManager.getInstace(this).startLocation(false);
            Util.showToast(this, "开始定位...");
            return null;
        }else
        if(mamager.isLocating())
        {
            Util.showToast(this, "正在定位...");
            return null;
        }else
        if(mamager.getMyLoation() == null)
        {
            Util.showToast(this, "定位失败...");
            return null;
        }
        Location loc = mamager.getMyLoation();
        FindShopAction action = new FindShopAction(loc.getLongitude(),loc.getLatitude(),
                                    false, 1, 10);
        action.setActionListener(new FindShopAction.ActionListener<ShopList>()
        {
            @Override
            public void onSucceed(ShopList result)
            {                
                if( result != null && result.shopList != null && result.shopList.size() > 0)
                {
                    showResult.setText(result.shopList.get(0).shopName);
                }else
                {
                    showResult.setText(" no result!");
                }
            }

            @Override
            public void onError(int resultCode)
            {
                showResult.setText(resultCode+"");
            }
        });
        return action;
    }
    // 查询商家详细
    protected SoapAction<?> testShopDetailAction()
    {
        ShopDetailAction action = new ShopDetailAction(6537,2);
        action.setActionListener(new ShopDetailAction.ActionListener<ShopDetail>()
        {
            @Override
            public void onSucceed(ShopDetail result)
            {      
                if(result != null)
                {
                    showResult.setText(result.shopName);
                }else
                {
                    showResult.setText(" no result!");
                }
            }

            @Override
            public void onError(int resultCode)
            {
                showResult.setText(resultCode+"");
            }
        });
        return action;
    }
    // 查询商家详细
    protected SoapAction<?> testshopDisCountListAction()
    {
        MyBMapManager mamager = MyBMapManager.getInstace(this);
        if(!mamager.isLocating() && mamager.getMyLoation() == null)
        {
            MyBMapManager.getInstace(this).startLocation(false);
            Util.showToast(this, "开始定位...");
            return null;
        }else
        if(mamager.isLocating())
        {
            Util.showToast(this, "正在定位...");
            return null;
        }else
        if(mamager.getMyLoation() == null)
        {
            Util.showToast(this, "定位失败...");
            return null;
        }
        Location loc = mamager.getMyLoation();
        ShopDisCountListAction action = new ShopDisCountListAction(0, loc.getLongitude(),loc.getLatitude() );
        action.setActionListener(new ShopDisCountListAction.ActionListener<DisShopList>()
        {
            @Override
            public void onSucceed(DisShopList result)
            {       
                if(result != null)
                {
                    showResult.setText(result.list + "");
                }else
                {
                    showResult.setText(" no result!");
                }
            }

            @Override
            public void onError(int resultCode)
            {
                showResult.setText(resultCode+"");
            }
        });
        return action;
    }   
    // 查询商家评论列表
    protected SoapAction<?> testCommentListAction()
    {
        CommentListAction action = new CommentListAction(5481,1,2);
        action.setActionListener(new CommentListAction.ActionListener<CommentList>()
        {
            @Override
            public void onSucceed(CommentList result)
            {    
                if(result != null)
                {
                    showResult.setText(result.commentList + "");
                }else
                {
                    showResult.setText(" no result!");
                }
            }

            @Override
            public void onError(int resultCode)
            {
                showResult.setText(resultCode+"");
            }
        });
        return action;
    }
}